package org.dromara.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.Post;
import org.dromara.business.domain.bo.PostBo;
import org.dromara.business.domain.vo.PostVo;
import org.dromara.business.mapper.PostMapper;
import org.dromara.business.service.PostService;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.util.TwoQueryUtils;
import org.dromara.system.domain.SysUser;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 评论Service业务层处理
 *
 * @author Lion Li
 * @date 2025-03-20
 */
@RequiredArgsConstructor
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    private final PostMapper baseMapper;
    private final ISysUserService userService;

    /**
     * 查询评论
     */
    @Override
    public PostVo queryById(Long postId) {
        return baseMapper.selectVoById(postId);
    }

    /**
     * 查询评论列表
     */
    @Override
    public TableDataInfo<PostVo> queryPageList(PostBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Post> lqw = buildQueryWrapper(bo);
        Page<PostVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        if (CollectionUtil.isEmpty(result.getRecords())) {
            return TableDataInfo.build(result);
        }
        //发布人信息
        TwoQueryUtils.setField(result.getRecords(), PostVo::getCreateBy, SysUser::getUserId, userService, PostVo::setUser);
        for (PostVo record : result.getRecords()) {
            record.setUserVo(BeanUtil.toBean(record.getUser(), SysUserVo.class));
            record.setUser(null);
            record.setCreateTimeStr(DateUtils.getTimeStr(record.getCreateTime()));
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询评论列表
     */
    @Override
    public List<PostVo> queryList(PostBo bo) {
        LambdaQueryWrapper<Post> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Post> buildQueryWrapper(PostBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Post> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getTitle()), Post::getTitle, bo.getTitle());
        lqw.eq(StrUtil.isNotBlank(bo.getImgId()), Post::getImgId, bo.getImgId());
        lqw.eq(bo.getWatchId() != null, Post::getWatchId, bo.getWatchId());
        lqw.eq(bo.getWaiterId() != null, Post::getWaiterId, bo.getWaiterId());
        lqw.eq(bo.getOrderId() != null, Post::getOrderId, bo.getOrderId());
        lqw.orderByDesc(Post::getCreateTime);
        return lqw;
    }

    /**
     * 新增评论
     */
    @Override
    public Boolean insertByBo(PostBo bo) {
        Post add = MapstructUtils.convert(bo, Post.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPostId(add.getPostId());
        }
        return flag;
    }

    /**
     * 修改评论
     */
    @Override
    public Boolean updateByBo(PostBo bo) {
        Post update = MapstructUtils.convert(bo, Post.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Post entity) {

    }

    /**
     * 批量删除评论
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
